package gulimall.controller;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import gulimall.entity.CategoryBrandRelation;
import gulimall.service.CategoryBrandRelationService;
import gulimall.service.IBrandService;
import gulimall.service.ICategoryService;
import gulimall.vo.BrandRespVo;
import org.springframework.web.bind.annotation.*;

import gulimall.utils.R;

import javax.annotation.Resource;


/**
 * 品牌分类关联
 *
 * @author HLF
 * @email 1132814637@qq.com
 * @date 2022-05-04 15:04:15
 */
@RestController
@RequestMapping("/product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Resource
    private CategoryBrandRelationService categoryBrandRelationService;

    @Resource
    private IBrandService brandService;

    @Resource
    private ICategoryService categoryService;

    /**
     * 获取品牌关联的分类
     * 接口文档：
     * GET请求：/product/categorybrandrelation/catelog/list
     * 请求的参数：brandId long 品牌id
     * <p>
     * {
     * "msg": "success",
     * "code": 0,
     * "data": [{
     * "catelogId": 0,
     * "catelogName": "string",
     * }]
     * }
     */
    @RequestMapping("/catelog/list")
    //@RequiresPermissions("product:categorybrandrelation:list")
    public R list(@RequestParam String brandId) {
        List<CategoryBrandRelation> relation = categoryBrandRelationService.list(
                new QueryWrapper<CategoryBrandRelation>()
                        .lambda()
                        .eq(CategoryBrandRelation::getBrandId, Long.valueOf(brandId))
        );
        return R.ok().put("data", relation);
    }

    /**
     * 获取分类关联的品牌
     * /product/categorybrandrelation/brands/list
     * 请求参数：catId  Long  分类id
     * 响应数据：
     * {
     * "msg": "success",
     * "code": 0,
     * "data": [{
     * "brandId": 0,
     * "brandName": "string",
     * }]
     * }
     */
    @GetMapping("/brands/list")
    public R brandList(@RequestParam(defaultValue = "0") Long catId) {
        List<BrandRespVo> list = categoryBrandRelationService.listBrandsByCatId(catId);
        return R.ok().put("data", list);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("product:categorybrandrelation:info")
    public R info(@PathVariable("id") Long id) {
        CategoryBrandRelation categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     * 请求参数：{"brandId":1,"catelogId":2}
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:categorybrandrelation:save")
    public R save(@RequestBody CategoryBrandRelation categoryBrandRelation) {

        categoryBrandRelationService.saveDetail(categoryBrandRelation);

        categoryBrandRelationService.save(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:categorybrandrelation:update")
    public R update(@RequestBody CategoryBrandRelation categoryBrandRelation) {
        categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:categorybrandrelation:delete")
    public R delete(@RequestBody List<Long> ids) {
        categoryBrandRelationService.removeByIds(ids);

        return R.ok();
    }

}
